﻿using System.Configuration;
using System.Linq;

namespace EP.Base.BaseService.Manager.Config
{
    public class IndexTaskServiceConfigSection : ConfigurationSection
    {
        public static string SectionName
        {
            get { return "IndexTaskServiceConfig"; }
        }

        [ConfigurationProperty("IndexTaskServiceConfigs", IsDefaultCollection = true)]
        public IndexTaskServiceConfigs IndexTaskServiceConfigs
        {
            get { return (IndexTaskServiceConfigs)this["IndexTaskServiceConfigs"]; }
        }
        
        
    }
    [ConfigurationCollection(typeof(IndexTaskServiceConfig), AddItemName = "IndexTaskServiceConfig")]
    public class IndexTaskServiceConfigs : ConfigurationElementCollection
    {

        protected override ConfigurationElement CreateNewElement()
        {
            return new IndexTaskServiceConfig();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((IndexTaskServiceConfig)element).Type;
        }
    }
    /// <summary>
    /// 索引信息配置
    /// </summary>
    public class IndexTaskServiceConfig : ConfigurationElement
    {
        /// <summary>
        /// 类型
        /// </summary>
        [ConfigurationProperty("Type", IsKey = true)]
        public short Type
        {
            get
            {
                return (short)this["Type"];
            }
            set { this["Type"] = value; }
        }
        /// <summary>
        /// 服务名
        /// </summary>
        [ConfigurationProperty("ServiceName")]
        public string ServiceName
        {
            get { return (string)this["ServiceName"]; }
            set { this["ServiceName"] = value; }
        }
    }
    /// <summary>
    ///操作
    /// </summary>
    public class IndexTaskServiceConfigHelper
    {
        /// <summary>
        /// 索引索引信息配置
        /// </summary>
        public static IndexTaskServiceConfigs IndexConfigs
        {
            get
            {
                return ((IndexTaskServiceConfigSection)ConfigurationManager.GetSection(IndexTaskServiceConfigSection.SectionName)).IndexTaskServiceConfigs;
            }
        }
        /// <summary>
        /// 根据索引类型得到对应的配置
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static IndexTaskServiceConfig IndexConfig(short type)
        {
            return IndexConfigs.Cast<IndexTaskServiceConfig>().FirstOrDefault(item => item.Type== type);
        }
    }
}
